package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.feign.ApAuthorFeign.ArticleFeign;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class ApUserRelationServiceImpl implements ApUserRelationService {
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    ApUserMapper apUserMapper;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //参数检查
        if(dto==null || (dto.getOperation()!=0&&dto.getOperation()!=1)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取作者信息
        ApAuthor apAuthor = articleFeign.selectById(dto.getAuthorId());
        if(apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"作者信息不存在");
        }
        ApUser followUser = apUserMapper.selectById(apAuthor.getUserId());
        if(followUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"作者信息不存在");
        }
        if(AppThreadLocalUtils.getUser()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"登录用户不存在");
        }
        //获取当前登录用户
        ApUser currentapUser = apUserMapper.selectById(AppThreadLocalUtils.getUser().getId());
        if(currentapUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"登录用户不存在");
        }
        //如果operation为0 关注作者
        if(dto.getOperation()==0){
            return followAuthor(followUser,currentapUser);
        }
        //如果operation为1 取关
        if(dto.getOperation()==1){
            return cancelFollowAuthor(followUser,currentapUser);
        }
        return null;
    }

    @Autowired
    ApUserFollowMapper apUserFollowMapper;
    @Autowired
    ApUserFanMapper apUserFanMapper;
    private ResponseResult cancelFollowAuthor(ApUser followUser, ApUser currentapUser) {
        //处理关注表信息
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(
                Wrappers.<ApUserFollow>lambdaQuery()
                        .eq(ApUserFollow::getId, currentapUser.getId())
                        .eq(ApUserFollow::getFollowId, followUser.getId())
        );
        if(apUserFollow==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"您还未关注该作者");
        }
        //删除关注信息
        apUserFollowMapper.deleteById(apUserFollow.getId());
        //处理粉丝表信息 删除对应粉丝信息
        apUserFanMapper.delete(
                Wrappers.<ApUserFan>lambdaQuery()
                .eq(ApUserFan::getUserId,followUser.getId())
                .eq(ApUserFan::getFansId,currentapUser.getId())
        );
        return ResponseResult.okResult();
    }
      //关注作者
    private ResponseResult followAuthor(ApUser followUser, ApUser currentapUser) {
        //检查有没有关注过这个作者
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(
                Wrappers.<ApUserFollow>lambdaQuery()
                        .eq(ApUserFollow::getUserId, currentapUser.getId())
                        .eq(ApUserFollow::getFollowId, followUser.getId())
        );
        //如果关注过 提示
        if(apUserFollow!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已关注");
        }
        //如果没关注，添加一条关注信息
         apUserFollow=new ApUserFollow();
        apUserFollow.setUserId(currentapUser.getId());
        apUserFollow.setFollowId(followUser.getId());
        apUserFollow.setFollowName(followUser.getName());
        apUserFollow.setLevel((short)0);
        apUserFollow.setIsNotice(false);
        apUserFollow.setCreatedTime(new Date());
        apUserFollowMapper.insert(apUserFollow);
        //添加粉丝信息
        ApUserFan apUserFan = apUserFanMapper.selectOne(
                Wrappers.<ApUserFan>lambdaQuery()
                        .eq(ApUserFan::getFansId, currentapUser.getId())
                        .eq(ApUserFan::getUserId, followUser.getId())
        );
        if(apUserFan==null){
            apUserFan=new ApUserFan();
            apUserFan.setUserId(followUser.getId());
            apUserFan.setFansId(Long.valueOf(currentapUser.getId()));
            apUserFan.setFansName(currentapUser.getName());
            apUserFan.setLevel((short)0);
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay(false);
            apUserFan.setIsShieldLetter(false);
            apUserFan.setIsShieldComment(false);
            apUserFanMapper.insert(apUserFan);
        }
        return ResponseResult.okResult();
    }
}
